﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WindowsService.Common;

namespace WindowsService.BusinessServices
{
    public class TaskStart
    {

        /// <summary>
        /// 业务开始运行
        /// </summary>
        public void TaskProcessing()
        {
            LogHelper.Write("Start", ".....服务正式运行.....");
            Thread.Sleep(1000 * 5 * 1); //在5秒内进行附加进程
            try
            {
                bool isRun = false; //默认不执行
                while (true)
                {
                    string hour = DateTime.Now.ToString("HH"); //获得当前的时间
                    isRun = Utility.TimeOut(hour) ? true : false;
                    if (isRun)//判断服务是否运行
                    {
                        #region 具体业务

                        LogHelper.Write("具体业务", LogHelper.GetExceptionMessage(MethodBase.GetCurrentMethod(), null, Guid.NewGuid().ToString()));

                        #endregion


                        isRun = false; //已经操作一次
                        Thread.Sleep(1000 * 60 * 62);   //休眠 62 分钟  //必须要超过 一个 小时  
                    }
                    else
                    {
                        //睡眠两分钟
                        Thread.Sleep(1000 * 60 * 2);  //停止设定时间，精确度比Sleep高
                    }
                }
            }
            catch (Exception ce)
            {
                LogHelper.Write("Operation", LogHelper.GetExceptionMessage(MethodBase.GetCurrentMethod(), ce, "\r\n\r\n.....服务异常.....\r\n\r\n"));

                ServiceController service = new ServiceController(new StartService().ServiceName);
                service.Stop(); //停止服务
                //service.Pause();//暂停服务
                //service.Start();//开始服务
            }
        }
    }
}
